package com.example.hoasbackend.web.controller;

import java.io.IOException;
import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
import com.example.hoasbackend.common.JsonResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@Controller
@RequestMapping("/api2/covid/")
public class CovidController {

    @RequestMapping(path = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addImage(@RequestParam("file") MultipartFile file)throws Exception {
        try {
            file.transferTo(new java.io.File("E:\\文件上传测试地址\\"+file.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
       System.out.println("已上传");
        return JsonResponse.success("succese");
    }
    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse imageList()throws Exception {
        //获取项目classes/static的地址
        String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
        //图片访问URI(即除了协议、地址和端口号的URL)
        String url_path = path+"/"+"image";
        File[] files=new File(url_path).listFiles();
        System.out.println(url_path);
        List<String> fileName=new ArrayList<>();
        for(File file:files){
            fileName.add(file.getName());
        }
        return JsonResponse.success(fileName);
    }
}